/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ws.websk.chatEndpoint2;

import ws.websk.chatEndpoint2.messages.ChatMessage;
import java.io.StringWriter;
import java.math.BigDecimal;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

/**
 *
 * @author Jens-Marius Nergård Hansen
 */
public class ChatMsgEncoder implements Encoder.Text<ChatMessage> {

    @Override
    public String encode(ChatMessage object) throws EncodeException {
        JsonObject jsonObject = Json.createObjectBuilder()
                .add("type", "chat")
                .add("message",object.getMessage())
                .add("nickname", object.getNickname())
                .build();

        StringWriter stWriter = new StringWriter();
        JsonWriter jsonWriter = Json.createWriter(stWriter);
        jsonWriter.writeObject(jsonObject);
        jsonWriter.close();

        String jsonData = stWriter.toString();
        return jsonData;    
    }

    @Override
    public void init(EndpointConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
